﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace EducationManagementSystem.Zr
{
    public partial class BanJi : System.Web.UI.Page
    {
        static string schoolID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {

            SqlConnection conn = DB.Conn();
            conn.Open();
            string sql = "select b.schoolid,b.schoolName,c.classID,c.className from Zhuren a,School b,Class c where a.schoolID=b.schoolID and b.schoolID=c.schoolID and zhurenID='" + Session["zhurenid"].ToString() + "'";
            SqlCommand cmd = new SqlCommand(sql, conn);
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(ds);
            schoolID = ds.Tables[0].Rows[0][0].ToString();
            TDtitie.InnerHtml = ds.Tables[0].Rows[0][1].ToString() + "的班级列表";
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (txtBm.Text == "")
            {

                this.Terr.InnerHtml = "<font color='red'>班级名称不能为空</font>";//前台定义的
                return;
            }
            else
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "insert into Class(classname,schoolid) values('" + txtBm.Text + "','" + schoolID + "')";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 添加成功 '); </script> ");

                    Bind();
                    this.Terr.InnerHtml = "";
                }
                else
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 添加失败 '); </script> ");
                }
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string s = e.CommandArgument.ToString();
            if (e.CommandName == "de")
            {
                SqlConnection conn = DB.Conn();
                conn.Open();
                string sql = "delete from class where classid='" + s + "' ";
                SqlCommand cmd = new SqlCommand(sql, conn);
                int a = cmd.ExecuteNonQuery();
                if (a > 0)
                {
                    this.Page.RegisterStartupScript(" ", "<script>alert(' 删除成功 '); </script> ");
                    Bind();
                    this.Terr.InnerHtml = "";
                }
                else
                {

                    this.Page.RegisterStartupScript(" ", "<script>alert('删除失败'); </script> ");
                }
            }
        }
    }
}